package com.wjm.servlet;

import com.wjm.http.HttpRequest;
import com.wjm.http.HttpResponse;
import com.wjm.vo.User;
import sun.util.resources.sr.CurrencyNames_sr_RS;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * Data:2022年08月16日
 * Author:wjm
 * Content:
 */
public class ShowAllDataServlet {
    //创建一个集合用于存储读User类的对象
    List<User> users = new ArrayList<>();

    public void service(HttpRequest request, HttpResponse response){
        //读取user.txt中的内容
        try {
            try(RandomAccessFile randomAccessFile = new RandomAccessFile("user.txt","r")){

                //创建一个字节长度为32字节的字节数组
                byte[] bytes = new byte[32];
                //因为一个用户的所有数组都是100字节，所以文件的长度/100就是用户的个数
                for(int i=0;i< randomAccessFile.length()/100;i++){
                    //读取username
                    randomAccessFile.read(bytes);
                    //将字节数组转化为字符串
                    String username = new String(bytes,"utf-8").trim();

                    //读取password
                    randomAccessFile.read(bytes);
                    //将字节数组转化为字符串
                    String password = new String(bytes,"utf-8").trim();

                    //读取sex
                    randomAccessFile.read(bytes);
                    //将字节数组转化为字符串
                    String sex = new String(bytes,"utf-8").trim();

                    //读取age
                    int age = randomAccessFile.readInt();

                    //创建User类对象，将每个用户的信息存入到对象的属性中
                    User user = new User();
                    user.setUsername(username);
                    user.setPassword(password);
                    user.setSex(sex);
                    user.setAge(age);
                    //将对象存储到users集合中
                    users.add(user);

                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("user集合"+users);
        //获取输出流
        PrintWriter printWriter = response.getPrintWriter();
        printWriter.println("<!DOCTYPE html>\n");
        printWriter.println("<html lang=\"en\">\n");
        printWriter.println("<head>\n" );
        printWriter.println("    <meta charset=\"UTF-8\">\n");
        printWriter.println("    <title>查询页面</title>\n" );
        printWriter.println("</head>\n" );
        printWriter.println("<body>\n" );
        printWriter.println("<div>\n" );
        printWriter.println("    <center>\n" );
        printWriter.println("        <h1>用户信息表</h1>\n" );
        printWriter.println("        <table border=\"2\">\n" );
        printWriter.println("            <tr>\n" );
        printWriter.println("                <td>\n" );
        printWriter.println("                    用户名\n" );
        printWriter.println("                </td>\n" );
        printWriter.println("                <td>\n" );
        printWriter.println("                    密码\n");
        printWriter.println("                </td>\n" );
        printWriter.println("                <td>\n" );
        printWriter.println("                    性别\n" );
        printWriter.println("                </td>\n" );
        printWriter.println("                <td>\n" );
        printWriter.println("                    年龄\n" );
        printWriter.println("                </td>\n" );
        printWriter.println("                <td>\n" );
        printWriter.println("                    操作\n" );
        printWriter.println("                </td>\n" );
        printWriter.println("            </tr>\n" );

        //遍历users集合，将users集合中的对象的属性循环输出
        for (User u: users){
            printWriter.println("            <tr>\n" );
            printWriter.println("                <td>"+u.getUsername()+"</td>\n" );
            printWriter.println("                <td>"+u.getPassword()+"</td>\n" );
            printWriter.println("                <td>"+u.getSex()+"</td>\n" );
            printWriter.println("                <td>"+u.getAge()+"</td>\n" );
            printWriter.println("                <td> <a href='update.do?username="+u.getUsername()+"'>修改用户数据</a></td>\n" );
            printWriter.println("            </tr>\n" );
        }

        printWriter.println("        </table>\n" );
        printWriter.println("    </center>\n" );
        printWriter.println("</div>\n" );
        printWriter.println("</body>\n" );
        printWriter.println("</html>");

    }




}
